package com.tybbank.Base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tybbank.R;
import com.tybbank.Utils.DensityUtil;
import com.tybbank.Utils.LocalManageUtil;


/**
 * Created by Administrator on 2017/6/30.
 */

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LocalManageUtil.setLocal(newBase));
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // StatusBarCompat.setStatusBarColor(this, R.color.backgroud, true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  //锁死 竖屏
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

    }


    //1.只有标题
    public void toolbar(Toolbar toolbar, String title) {
        toolbar(toolbar, title, 0, null);
    }

    //2.只有左边返回箭头和标题
    public void toolbar(Toolbar toolbar, String title, int left) {
        toolbar(toolbar, title, left, null);
    }

    //3.只有左边返回箭头、标题、右边操作图标
    public void toolbar(Toolbar toolbar, String title, int left, int right) {
        toolbar1(toolbar, title, left, right);
    }

    //4.只有左边返回箭头、标题、右边操作文本
    public void toolbar(Toolbar toolbar, String title, int left, String right) {
        toolbar1(toolbar, title, left, right);
    }

    public void toolbar1(Toolbar toolbar, String title, int left, Object right) {
        //5.0系统以上，获取状态栏高度，重新设置
        if (Build.VERSION.SDK_INT >= 21) {
            //获取系统状态栏高度
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
//            KLog.e("状态栏高度",statusBarHeight+"");
            toolbar.setPadding(0, statusBarHeight, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(this, 50) + statusBarHeight);
            toolbar.setLayoutParams(params);
        }
        TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        final TextView mRight = (TextView) toolbar.findViewById(R.id.toolbar_right);
        final ImageButton ivLeft = (ImageButton) toolbar.findViewById(R.id.toolbar_close);
        //标题
        mTitle.setText(title);
        //左边
        if (left != 0) {
            ivLeft.setImageResource(left);
            ivLeft.setVisibility(View.VISIBLE);
            ivLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LeftClose();
                }
            });
        }
        //右边
    if (right instanceof String) {
            //文字
            mRight.setVisibility(View.VISIBLE);
            mRight.setText((String) right);
            mRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RightNext();
                }
            });
        }
    }

    //toolbar右边的操作
    public void RightNext() {

    }
    public void showTips(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
    //toolbar左侧键的操作
    public void LeftClose() {
        this.finish();
    }

    public PackageInfo getPackageInfo() {
        PackageManager packageManager = getPackageManager();
        PackageInfo info = null;
        try {
            info = packageManager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return info;
    }
}
